package com.sh_rw.label;

import com.sh_rw.label.bean.BaseLabel;
import com.sh_rw.label.common.arg.PortArg;
import com.sh_rw.label.common.black.Black;
import com.sh_rw.label.common.factory.ParserFactory;
import com.sh_rw.label.common.parser.Parser;
import com.sh_rw.label.listener.ParserListener;

import java.util.HashMap;

public class LabelPerser {

    private Parser parser;
    private Black mBlack;
    private ParserListener mParserListener;
    private HashMap<String, Parser> mParserCache;

    private LabelPerser(Builder builder) {
        this.parser = builder.parser;
        this.mBlack = builder.mBlack;
        this.mParserListener = builder.mParserListener;
        mParserCache = new HashMap<>();
    }

    public static class Builder {
        private Parser parser;
        private Black mBlack;
        private ParserListener mParserListener;

        public LabelPerser build() {
            return new LabelPerser(this);
        }

        /**
         * 设置解析监听
         *
         * @param parserListener
         * @param <T>
         * @return
         */
        public <T extends BaseLabel> Builder setParserListener(ParserListener<T> parserListener) {
            this.mParserListener = parserListener;
            return this;
        }

        public Builder setParser(Parser parser) {
            this.parser = parser;
            return this;
        }

        /**
         * 设置密码规则
         *
         * @param rules
         * @return
         */
        public Builder setRules(Black rules) {
            mBlack = rules;
            return this;
        }
    }

    /**
     * 设置解析监听
     *
     * @param parserListener
     * @param <T>
     * @return
     */
    public <T extends BaseLabel> void setParserListener(ParserListener<T> parserListener) {
        this.mParserListener = parserListener;

    }

    public void setParser(Parser parser) {
        this.parser = parser;
    }

    /**
     * 设置密码规则
     *
     * @param rules
     * @return
     */
    public void setRules(Black rules) {
        mBlack = rules;
    }

    /**
     * 解析
     *
     * @param bytes
     */
    public void analysis(byte[] bytes) {
        boolean qualified = CheckUtils.checkPacket(bytes);
        if (qualified) {
            //加密处理
            bytes = mBlack.onBlackHandler(bytes);
            int cmd = bytes[10] & 0xff;
            int type = bytes[13] & 0xff;

            parser = getParser(cmd, type);
            if (parser != null) {
                parser.onLabelParser(bytes, mParserListener);
            }
        }
    }

    /**
     * 获取解析器
     *
     * @param cmd
     * @param type
     * @return
     */
    private Parser getParser(int cmd, int type) {
        //获取解析器
        Parser parser = null;
        switch (cmd) {
            case PortArg.CMD_LABEL_R:
                if (type == 18) {
                    parser = ParserFactory.get().getParser(ParserFactory.PARSER_18_TYPE);
                } else if (type == 19) {
                    parser = ParserFactory.get().getParser(ParserFactory.PARSER_19_TYPE);
                } else if (type == 20){
                    parser = ParserFactory.get().getParser(ParserFactory.PARSER_19_TYPE);
                } else if (type == 30) {
                    parser = ParserFactory.get().getParser(ParserFactory.PARSER_30_TYPE);
                } else {

                }
                break;
            case PortArg.CMD_READ_RF://
                parser = ParserFactory.get().getParser(ParserFactory.PARSER_RF_TYPE);
                break;

            case PortArg.CMD_BASE_READ_ARG:
                parser = ParserFactory.get().getParser(ParserFactory.PARSER_BASE_ARG_TYPE);
                break;
            case PortArg.CMD_CONFIG_RF://配置回复4E47,4F4B
            case PortArg.CMD_BASE_CONFIG_ARG:
                parser = ParserFactory.get().getParser(ParserFactory.PARSER_REPLY_TYPE);
                break;
            default:
                break;
        }
        return parser;
    }

}
